/* Copyright (c) 2012 Nordic Semiconductor. All Rights Reserved.
 *
 * The information contained herein is property of Nordic Semiconductor ASA.
 * Terms and conditions of usage are described in detail in NORDIC
 * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
 *
 * Licensees are granted free, non-transferable use of the information. NO
 * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
 * the file.
 *
 * $LastChangedRevision$
 */

/** @file
 *
 * @defgroup ble_flash_module Flash Manager
 * @{
 * @ingroup ble_sdk_lib
 * @brief Module for accessing flash memory.
 *
 * @details It contains functions for reading, writing and erasing one page in flash.
 *
 *          The module uses the first 32 bits of the flash page to write a magic number in order to
 *          determine if the page has been written or not.
 *
 * @note Be careful not to use a page number in the SoftDevice area (which currently occupies the
 *       range 0 to 127), or in your application space! In both cases, this would end up
 *       with a hard fault.
 */

#ifndef BLE_FLASH_H__
#define BLE_FLASH_H__

#include <stdint.h>

/**@brief Initializes the flash manager.
 *
 * @param[in]  elem_size  Size, in number of bytes, of a single element to handle in flash.
 */
void ble_flash_init(uint8_t elem_size);

/**@brief Writes the flash at the given page with the given data.
 *
 * @note This operation blocks the CPU, so it should not be done while the radio is running!
 *
 * @param[in]  page_num            Page number to update.
 * @param[in]  p_in_array          Pointer to a RAM area containing the elements to write in flash.
 *                                 This area has to be 32 bits aligned.
 * @param[out] elem_count          Number of elements to write in flash.
 * @return     NRF_SUCCESS on successful flash write, otherwise an error code.
 */
uint32_t ble_flash_write(uint8_t page_num, uint32_t * p_in_array, uint8_t elem_count);

/**@brief Reads data from flash to RAM.
 *
 * @param[in]  page_num            Page number to read.
 * @param[out] p_out_array         Pointer to a RAM area where the found data will be written. 
 *                                 This area has to be 32 bits aligned.
 * @param[out] elem_count          Number of elements read.
 * @return     NRF_SUCCESS on successful upload, NRF_ERROR_NOT_FOUND if no valid data has been found
 *             in flash (first 32 bits not equal to the MAGIC_NUMBER.
 */
uint32_t ble_flash_read(uint8_t page_num, uint32_t * p_out_array, uint8_t * p_elem_count);

/**@brief Erases a flash page.
 *
 * @note This operation blocks the CPU, so it should not be done while the radio is running!
 *
 * @param[in]  page_num            Page number to erase.
 */
void ble_flash_page_erase(uint8_t page_num);

#endif // BLE_FLASH_H__

/** @} */
